local fuhan = fk.CreateSkill {
  name = "gai__fuhan",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable{
  ["gai__fuhan"] = "扶汉",
  [":gai__fuhan"] = "限定技，准备阶段，你可以移去所有「梅影」标记并摸等量的牌，然后从五个蜀势力技能中"..
  "选择并获得一个技能（执行两次）。若此时你是体力值最低的角色，你回复1点体力。",

  ["#gai__fuhan-invoke"] = "扶汉：你可以移去「梅影」标记，获得等量蜀势力武将的两个技能！",
  ["#gai__fuhan-choice"] = "扶汉：选择你要获得的技能（第%arg次选择）",

  ["$gai__fuhan1"] = "逝者如斯，目古长流，唯英烈之魂悬北斗而长存！",
  ["$gai__fuhan2"] = "赵氏之女，跪祈诸公勿渡黄泉，暂留人间、佑大汉万年！",
}

-- 蜀势力技能池
local shu_skill_pool = {
  "ex__rende", "ex__paoxiao", "ex__wusheng", "$ex__yijue", "ex__guanxing", "kongcheng", "ex__jizhi", "ex__qicai", "ex__tieqi", "mashu",
  "danxinl", "js__fengxiang" , "jilis", "ofl_shiji__boming", "ofl_shiji__ejian", "ofl__sangu", "yizu", "youjun", "fenwu", "qingkou",
  "zhongen", "liebao", "yuantao", "conglong", "xianwu", "zhijie", "mobile__shushen", "yizan", "xiantu", "qiangzhi", "mobile__jiaohua",
  "zuilun", "fuyin", "liangzhu", "bingzheng", "sheyan", "qianya", "shuimeng", "ty__fengpo", "zongshij", "$ol_ex__qiaoshui", "fuhun",
  "jianzhengq", "zhuandui", "tianbian", "wangliec", "fhyx__duliang", "fhyx__fulin", "ty__wuniang", "xiuhao", "sujian", "chenglie",
  "xiaosi", "dianjun", "kangrui", "ol_ex__tiaoxin", "ol__xuehen", "ol__huxiao", "ol__sanyao", "ty_ex__zhiman", "ol_ex__liegong",
  "ol_ex__kuanggu", "xiangle", "ol_ex__fangquan", "ol_ex__lianhuan", "bazhen", "ol_ex__huoji", "ol_ex__kanpo", "cangzhuo", "ty_ex__xuanhuo",
  "ty_ex__enyuan", "ol_ex__dangxian", "juxiang", "lieren", "changbiao", "goude", "ol__shenxian", "qiangwu", "zhenying", "ty__baobian",
  "ty__wenji", "ty__tunjiang", "ty__qirang", "ty__yuhua", "manyi", "mansi", "souying", "ty__fengshih", "tingxian", "benshi", "ty__qinqing",
  "huisheng", "cunwei", "qiao", "chengshang", "ty_ex__benxi", "ty_ex__qiaoshi", "ty_ex__yanyu", "zhanjue", "ty_ex__wurong", "ty_ex__shizhi",
  "ty_ex__fuman", "ty_ex__zhuhai", "ty_ex__qianxi", "ty_ex__zhongyong", "shiming", "jiangxi"
}

local U = require "packages/utility/utility"

fuhan:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(fuhan.name) and player.phase == Player.Start and
      player:getMark("@meiying") > 0 and player:usedSkillTimes(fuhan.name, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = fuhan.name,
      prompt = "#gai__fuhan-invoke",
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = player:getMark("@meiying")
    room:setPlayerMark(player, "@meiying", 0)
    player:drawCards(n, fuhan.name)
    if player.dead then return end

    local available_skills = table.filter(shu_skill_pool, function(skill_name)
      local skill = Fk.skills[skill_name]
      return skill and not table.find({Skill.Limited, Skill.Wake, Skill.Quest, Skill.Lord}, function(tag)
        return skill:hasTag(tag)
      end)
    end)
    
    if #available_skills == 0 then return false end
    
    local skills_to_gain = {}
    
    for i = 1, 2 do
      if #available_skills == 0 then break end
      if player.dead then break end
      
      local num_to_select = math.min(5, #available_skills)
      local selected_skills = table.random(available_skills, num_to_select)
      
      -- 让玩家选择1个技能
      local choices = room:askToChoice(player, {
        choices = selected_skills,
        min_num = 1,
        max_num = 1,
        skill_name = fuhan.name,
        prompt = "#gai__fuhan-choice:" .. i,
        detailed = true,
      })
      
      if type(choices) == "string" then
        choices = {choices}
      end
      
      if #choices > 0 then
        table.insert(skills_to_gain, choices[1])
        available_skills = table.filter(available_skills, function(skill)
          return skill ~= choices[1]
        end)
      else
        break
      end
    end
    
    if #skills_to_gain > 0 then
      room:handleAddLoseSkills(player, table.concat(skills_to_gain, "|"))
    end

    if not player.dead and player:isWounded() then
      local isLowest = true
      for _, p in ipairs(room.alive_players) do
        if p ~= player and p.hp < player.hp then
          isLowest = false
          break
        end
      end
      
      if isLowest then
        room:recover{
          who = player,
          num = 1,
          recoverBy = player,
          skillName = fuhan.name,
        }
      end
    end
  end,
})

return fuhan